/*
 * @Author: 李环玉 834654733@qq.com
 * @Date: 2023-11-30 17:22:44
 * @LastEditors: 李环玉 834654733@qq.com
 * @LastEditTime: 2023-12-01 14:50:06
 * @FilePath: \koa-server\src\controller\admin.controller.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const jwt = require('jsonwebtoken')
const { getAdminInfo,getAdminOne,update,updatePass,getAllAdmin } = require('../service/admin.service')
const { success,error } = require('../utils/response')
const { JWT_SECRET } = require('../config/config.default')
class adminControllrt {
  async admin(ctx,next) {
    const params = ctx.request.body
    try{
      const res = await getAdminInfo({})
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      console.log(err)
      error(ctx,null)
    }
  }
  //登录管理系统
  async loginAdmin(ctx,next) {
    const params = ctx.request.body
    try{
      const {passWord,...res} = await getAdminOne(params)
      const token = jwt.sign(res,JWT_SECRET,{expiresIn:'1d'})
      success(ctx,{token:token,...res},'登录成功')
    } catch(err) {
      error(ctx,null,err,502)
    }
  }
  //修改博主信息
  async updateAdmin(ctx,next){
    const params = ctx.request.body
    try{
      const res = await update(params)
      if(res){
        success(ctx,null)
      } else {
        error(ctx,null)
      }
    }catch(err){
      console.log(err)
      error(ctx,null,err,502)
    }
  }
  //修改密码
  async resetPassWord(ctx,next){
    const params = ctx.request.body
    try{
      const res = await updatePass(params)
      if(res){
        success(ctx,null)
      } else {
        error(ctx,null)
      }
    }catch(err){
      console.log(err)
      error(ctx,null,err,502)
    }
  }
  async getAdminAll(ctx,next){
    const params = ctx.request.body
    try{
      const res = await getAllAdmin({})
      if(res){
        success(ctx,res)
      } else {
        error(ctx)
      }
    } catch(err)  {
      console.log(err)
      error(ctx,null)
    }
  }
}

module.exports = new adminControllrt()